#!/bin/bash

# 部署脚本，用于在不同环境中部署支付服务应用

# 显示帮助信息
show_help() {
    echo "支付服务部署脚本"
    echo "用法: $0 [选项]"
    echo "选项:"
    echo "  -e, --env ENV          指定部署环境 (dev, test, uat, prod), 默认: dev"
    echo "  -t, --tag TAG          指定镜像标签, 默认: latest"
    echo "  -r, --registry URL     指定Docker镜像仓库地址, 默认: registry.example.com"
    echo "  -p, --port PORT        指定应用端口, 默认: 8080"
    echo "  -d, --data-path PATH   指定数据存储路径, 默认: /data/payservice"
    echo "  -h, --help             显示帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 --env dev --tag v1.0.0"
    echo "  $0 --env prod --tag v1.0.0 --port 9090"
}

# 默认值
DEPLOY_ENV="dev"
IMAGE_TAG="latest"
DOCKER_REGISTRY="registry.example.com"
APP_PORT="8080"
DATA_PATH="/data/payservice"

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -e|--env)
            DEPLOY_ENV="$2"
            shift 2
            ;;
        -t|--tag)
            IMAGE_TAG="$2"
            shift 2
            ;;
        -r|--registry)
            DOCKER_REGISTRY="$2"
            shift 2
            ;;
        -p|--port)
            APP_PORT="$2"
            shift 2
            ;;
        -d|--data-path)
            DATA_PATH="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
done

# 验证环境参数
if [[ ! "$DEPLOY_ENV" =~ ^(dev|test|uat|prod)$ ]]; then
    echo "错误: 不支持的环境 '$DEPLOY_ENV'"
    echo "支持的环境: dev, test, uat, prod"
    exit 1
fi

# 设置环境特定的配置
case $DEPLOY_ENV in
    dev)
        MYSQL_ROOT_PASSWORD="123456"
        MYSQL_USER="root"
        MYSQL_PASSWORD="123456"
        REDIS_PASSWORD=""
        JAVA_OPTS="-Xms512m -Xmx1g"
        ;;
    test)
        MYSQL_ROOT_PASSWORD="test_password"
        MYSQL_USER="test_user"
        MYSQL_PASSWORD="test_password"
        REDIS_PASSWORD=""
        JAVA_OPTS="-Xms1g -Xmx2g"
        ;;
    uat)
        MYSQL_ROOT_PASSWORD="uat_password"
        MYSQL_USER="uat_user"
        MYSQL_PASSWORD="uat_password"
        REDIS_PASSWORD="uat_redis_password"
        JAVA_OPTS="-Xms2g -Xmx4g"
        ;;
    prod)
        MYSQL_ROOT_PASSWORD="prod_password"
        MYSQL_USER="prod_user"
        MYSQL_PASSWORD="prod_password"
        REDIS_PASSWORD="prod_redis_password"
        JAVA_OPTS="-Xms4g -Xmx8g -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
        ;;
esac

# 设置数据路径
LOG_PATH="${DATA_PATH}/logs"
MYSQL_DATA_PATH="${DATA_PATH}/mysql"
REDIS_DATA_PATH="${DATA_PATH}/redis"
MYSQL_INIT_PATH="./db"

# 创建必要的目录
mkdir -p "$LOG_PATH" "$MYSQL_DATA_PATH" "$REDIS_DATA_PATH"

echo "开始部署支付服务应用..."
echo "环境: $DEPLOY_ENV"
echo "镜像标签: $IMAGE_TAG"
echo "应用端口: $APP_PORT"
echo "数据路径: $DATA_PATH"

# 导出环境变量，供docker-compose使用
export DEPLOY_ENV
export IMAGE_TAG
export DOCKER_REGISTRY
export APP_PORT
export JAVA_OPTS
export DB_USERNAME=$MYSQL_USER
export DB_PASSWORD=$MYSQL_PASSWORD
export MYSQL_ROOT_PASSWORD
export MYSQL_DATABASE="payservice"
export MYSQL_USER
export MYSQL_PASSWORD
export MYSQL_PORT="3306"
export REDIS_PORT="6379"
export REDIS_PASSWORD
export LOG_PATH
export MYSQL_DATA_PATH
export REDIS_DATA_PATH
export MYSQL_INIT_PATH
export DATA_CENTER_ID="1"
export WORKER_ID="1"

# 拉取最新镜像
echo "拉取最新镜像..."
docker pull "$DOCKER_REGISTRY/payservice:$IMAGE_TAG"

# 使用docker-compose部署应用
echo "部署应用..."
docker-compose -f docker-compose-deploy.yml -p payservice down
docker-compose -f docker-compose-deploy.yml -p payservice up -d

# 检查应用是否成功启动
echo "检查应用状态..."
sleep 10
if docker ps | grep -q "payservice"; then
    echo "应用已成功启动"
    echo "访问地址: http://localhost:$APP_PORT/api"
else
    echo "应用启动失败，请检查日志"
    docker-compose -f docker-compose-deploy.yml -p payservice logs
    exit 1
fi

echo "部署完成" 